﻿using Dapper;
using SYJ.BLL;
using SYJ.Common.web;
using SYJ.IBLL;
using SYJ.Model;
using SYJ.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace SYJ.Logic.Controllers.admin
{
    public class SysLeavesMgrController : BaseController
    {
      ISysLeavesBLL bll = new SysLeavesBLL();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetSysLeavesList(QueryModel query, string title,int? leavetype)
        {
            long count = 0;
            if (!string.IsNullOrEmpty(title))
            {
                query.SqlWhere = " and a.title like ?title ";
                query.Param.Add("title", "%" + title + "%" );
            }
            if (leavetype != null && leavetype >= 0)
            {
                query.SqlWhere += " and a.type = ?type ";
                query.Param.Add("type", leavetype);
            }
            List<SysLeaves> list = bll.GetList(query, out count);
            var respose = JsonLayuiGridData<SysLeaves>.Create(query, list, count, "");
            return Json(respose, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult AddOrUp(int? id)
        {
            SysLeaves role = new SysLeaves();
            if (id != null)
            {
                var param = new DynamicParameters();
                param.Add("id", id);
                role = bll.GetModel("select a.*,b.nickname as create_author from sys_leaves a left join sys_user b on a.user_id=b.id where a.id=?id", param);
            }
            return View(role);
        }
        [HttpPost]
        public ActionResult AddOrUp(SysLeaves model)
        {
            if (model.id > 0)
            {
                SysLeaves leave = bll.GetModel(model.id);
               leave.status = model.status;
               leave.reply = model.reply;
               leave.reply_id = AppContext.Current.User.id;
               leave.reply_time = DateTime.Now.ToLocalTime();
               return Json(bll.Update(leave), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(bll.Insert(model), JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Delete(int id)
        {
            bool delete = bll.Delete(new SysLeaves() { id = id });
            return Json(delete, JsonRequestBehavior.AllowGet);
        }
    }
}
